#include "mainwindow.h"
#include "./ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->outboxAddressTextEdit->setPlaceholderText("请输入您的发件邮箱");
    ui->outboxPwdTextEdit->setPlaceholderText("请输入发件箱密码");
    ui->comparisonTextEdit->setPlaceholderText("请选择“邮箱与附件对照表”");

}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_enterSMTPconfigButton_clicked()
{
    ui->smtpTextEdit->setText("smtp.office365.com");
    ui->smtpPortTextEdit->setText("587");
}




void MainWindow::on_chooseAttachmentButton_clicked()
{
    QString selectDir = QFileDialog::getExistingDirectory();
    ui->attachmentFolderTextEdit->setText(selectDir);
}



void MainWindow::on_comparisonButton_clicked()
{
    QString filePath = QFileDialog::getOpenFileName(this,"选择文件","","邮箱附件对照表(*.xls *.xlsx)");
    ui->comparisonTextEdit->setText(filePath);
}

void MainWindow::on_sentButton_clicked()
{
    //检查是否有空选项
    if(checkStatus()==false)
        return;

    // SentEmail(con)

}


void MainWindow::on_showErrorMailButton_clicked()
{
    ui->stackedWidget->setCurrentIndex(1);
}


void MainWindow::on_backToMainPageButton_clicked()
{
    ui->stackedWidget->setCurrentIndex(0);
}

bool MainWindow::checkStatus()
{
    ui->outboxAddressTextEdit->setStyleSheet("");
    ui->outboxPwdTextEdit->setStyleSheet("");
    ui->smtpTextEdit->setStyleSheet("");
    ui->smtpPortTextEdit->setStyleSheet("");
    bool hasError = false;
    if(ui->outboxAddressTextEdit->toPlainText()=="")
    {
        ui->outboxAddressTextEdit->setStyleSheet("QTextEdit { border: 2px solid red; }");
        hasError = true;
    }
    if(ui->outboxPwdTextEdit->toPlainText()=="")
    {
        ui->outboxPwdTextEdit->setStyleSheet("QTextEdit { border: 2px solid red; }");
        hasError = true;
    }
    if(ui->smtpTextEdit->toPlainText()=="")
    {
        ui->smtpTextEdit->setStyleSheet("QTextEdit { border: 2px solid red; }");
        hasError = true;
    }
    if(ui->smtpPortTextEdit->toPlainText()=="")
    {
        ui->smtpPortTextEdit->setStyleSheet("QTextEdit { border: 2px solid red; }");
        hasError = true;
    }
    if (hasError) {
        return false;
    }
    QString account =ui->outboxAddressTextEdit->toPlainText();
    QString password  =ui->outboxPwdTextEdit->toPlainText();
    QString smtpclient  =ui->smtpTextEdit->toPlainText();
    QString smtpport =ui->smtpPortTextEdit->toPlainText();
    return true;
}

